package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.SearchService;
import entity.PageResult;
import entity.Result;

import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("article")
public class SearchController {

    @Autowired
    private SearchService searchService;

    //添加文章
    @PostMapping()
    public Result addArticle(@RequestBody Article article) {
//        try {
            searchService.addArticle(article);
            return new Result(true, StatusCode.OK, "新增成功");
//        } catch (Exception e) {
//            return new Result(false, StatusCode.ERROR, "新增失败");
//        }
    }


    //根据标题或内容进行文章的搜索
    @GetMapping("search/{keywords}/{page}/{size}")
    public Result findByTitleAndContentLike(@PathVariable int page, @PathVariable int size, @PathVariable String keywords) {
        PageResult<Article> articlePageResult = searchService.findByTitleAndContentLike(page, size, keywords);
        return new Result(true, StatusCode.OK, "根据关键字分页查询成功", articlePageResult);
    }
}
